package com.huanxing.cloud.auth.config;

import com.huanxing.cloud.common.core.constant.SecurityConstants;
import com.huanxing.cloud.security.entity.HuanxingUser;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/**
 * token增强
 *
 * @author lijx
 * @date 2022/5/2 13:43
 * @version 1.0
 */
public class HuanxingTokenEnhancer implements TokenEnhancer {
  @Override
  public OAuth2AccessToken enhance(
      OAuth2AccessToken accessToken, OAuth2Authentication oAuth2Authentication) {
    final Map<String, Object> additionalInfo = new HashMap<>();
    HuanxingUser huanxingUser =
        (HuanxingUser) oAuth2Authentication.getUserAuthentication().getPrincipal();
    additionalInfo.put(SecurityConstants.DETAILS_USER_ID, huanxingUser.getUserId());
    additionalInfo.put(SecurityConstants.DETAILS_USERNAME, huanxingUser.getUsername());
    ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
    return accessToken;
  }
}
